package com.cos.shop.web.filter; /**
 * @author: COS
 * @time: 2023/5/11 14:35
 * @description:
 */

import com.cos.shop.common.Const;
import com.cos.shop.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "Filter03_LoginRole", value = {"/cart","/addr","/order"})
public class Filter03_LoginRole implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //判断用户是否等了
        User user = (User) req.getSession().getAttribute(Const.LOGIN_INFO);
        if (user == null) {
            System.out.println("没有登录");
            resp.sendRedirect("/login.jsp");
        } else {
            chain.doFilter(req, resp);
        }
    }
}
